package com.app.gridview;

import android.app.Activity;
import android.content.Intent;
import android.os.Build;
import android.os.Bundle;
import android.view.View;
import android.view.ViewGroup;
import android.view.WindowManager;
import android.view.animation.Animation;
import android.view.animation.CycleInterpolator;
import android.view.animation.DecelerateInterpolator;
import android.view.animation.RotateAnimation;
import android.view.animation.TranslateAnimation;
import android.view.inputmethod.InputMethodManager;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.Toast;

import androidx.appcompat.app.AppCompatActivity;
import androidx.core.content.ContextCompat;

import com.zndroid.gridview.AGridImageViewAdapter;
import com.zndroid.gridview.GridImageViewLayout;
import com.zndroid.gridview.preview.GridImageViewClickAdapter;

import java.util.ArrayList;

public class MainActivity extends AppCompatActivity {
    private ImageView imageView, headImageView;


    private GridImageViewLayout layout;
    private int num = 3;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        initStatusBar();

        layout = findViewById(R.id.gv);
        imageView = findViewById(R.id.iv);
        headImageView = findViewById(R.id.iv_head);

        initData();
    }

    private void initData() {
        int[] images = new int[] {
                R.mipmap.t_1,
                R.mipmap.t_2,
                R.mipmap.t_3,
                R.mipmap.t_4,
                R.mipmap.t_5,
                R.mipmap.t_6,
                R.mipmap.t_7,
                R.mipmap.t_8,
                R.mipmap.t_9,
                R.mipmap.t_10,
                R.mipmap.t_11,
                R.mipmap.t_12,
                R.mipmap.t_13,
        };

        int[] images1 = new int[] {
                R.raw.t_1,
                R.raw.t_2,
                R.raw.t_3,
                R.raw.t_4,
                R.raw.t_5,
                R.raw.t_6,
                R.raw.t_7,
                R.raw.t_8,
                R.raw.t_9,
                R.raw.t_10,
                R.raw.t_11,
                R.raw.t_12,
                R.raw.t_13,
        };

        String head = "/sdcard/Huawei/MagazineUnlock/";
        String[] images3 = new String[] {
                //权限这里就不做单独处理了，demo手动先开一下
                head + "magazine-unlock-01-2.3.8642-1EF1875E2AF0503DC9F26EB8AF53E293.jpg",
                head + "magazine-unlock-01-2.3.8642-1224AFBBA52891D1DB7A42C160CAF5F5.jpg",
                head + "magazine-unlock-01-2.3.8632-EF1AF0A0B377FE4876AA3D6CFFEE7DC9.jpg",
                head + "magazine-unlock-01-2.3.8632-B3C5670FC770A8BF0C890159A59FDCB8.jpg",
                head + "magazine-unlock-01-2.3.8632-5D0A6C81122DB98CE7FECF17685F2EA4.jpg",
                head + "magazine-unlock-01-2.3.8632-337961FF2B35A27536763E2329230FD5.jpg",
                head + "magazine-unlock-01-2.3.8632-255393196A98E9CEFC80908701229A1A.jpg",
                head + "magazine-unlock-01-2.3.8632-09C2E844AB0254F93B7D965CCDC16B74.jpg",
                head + "magazine-unlock-01-2.3.8622-E517CB4587A88AE0B9F85FBB641D659E.jpg",
                head + "magazine-unlock-01-2.3.8622-B382615C804CB7A8D86A6E29636CBA39.jpg",
                head + "magazine-unlock-01-2.3.8622-AAAF84C15A191967A5196F3A716E3B16.jpg",
                head + "magazine-unlock-01-2.3.8622-8F7E880CD6522CDED6EF3A4635A6D078.jpg",
                head + "magazine-unlock-01-2.3.8492-034BBF6A51D31386E1A1D9241EA19FCA.jpg",
        };

        String[] images4 = new String[] {
            "https://pic.rmb.bdstatic.com/bjh/news/c95cc003c1798da3122a67627284cc9d.gif",
            "https://pics6.baidu.com/feed/6d81800a19d8bc3ea04bf1c67e307816a9d345bb.jpeg?token=e674c442323d581c96345b3b7d8d1213",
            "https://pics5.baidu.com/feed/43a7d933c895d143023bb28d884b5c0a5baf0717.jpeg?token=d331b7902cb4ed57dad5f857704670f6",
            "https://pics5.baidu.com/feed/5fdf8db1cb134954e4083d66a3f54c50d0094ae7.jpeg?token=96409f225dd1700eea8814800e5f7612",
            "https://pics2.baidu.com/feed/8718367adab44aedc371c73d4ba75909a08bfb75.jpeg?token=f0606868b4e6fb0f3d8dd078a93cec8a",
            "https://pics5.baidu.com/feed/9e3df8dcd100baa17dc73078b3ab671acafc2e4a.jpeg?token=8f224626f22cc1897e1662f735ff48a5",
            "https://pics1.baidu.com/feed/0df3d7ca7bcb0a468f5751fd92d8282c6960afc6.jpeg?token=2fec807596e804860a1ad5039d47e35e",
            "https://pics2.baidu.com/feed/738b4710b912c8fc5978840ef6bf4c4dd48821aa.jpeg?token=58becfc6e723ea26db6feb5b1ec940d9",
            "https://pics3.baidu.com/feed/6609c93d70cf3bc779f0f8ba2dbb64a9cc112a3c.jpeg?token=7d9c530cb0918d1aff39cb93b672aa3a",
            "https://pics6.baidu.com/feed/267f9e2f070828383920c9fb4d2277094d08f170.jpeg?token=f2903f4f558296db33426fc381280648",
            "https://pics6.baidu.com/feed/2fdda3cc7cd98d1035cc9e29e09066067aec90c2.jpeg?token=0247737163f335be686345e3f6a4a4d3",
            "https://pics0.baidu.com/feed/c83d70cf3bc79f3d7b4de8479d071319738b2943.jpeg?token=968f723acc55e3beacfc08b858801eb2",
            "https://pics5.baidu.com/feed/279759ee3d6d55fb1de902390598924221a4ddc1.jpeg?token=a26e5a0dfbfd24a9ecfd21f2fc6e7d57",
        };

        ArrayList<ImageInfo> arrayList = new ArrayList<>();

        for (int i = 0; i < num; i++) {
            ImageInfo info = new ImageInfo();
            info.setResId(images[i]);
//            info.setResId(images1[i]);
//            info.setFilePath(images3[i]);
//            info.setBigUrl(images4[i]);
//            info.setSmallUrl(images4[i]);
//覆盖图层实现：（指定资源）
            if (i == 0 || i == 2) {
                info.setCoverId(R.mipmap.common_play);
            }

            arrayList.add(info);
        }

        AGridImageViewAdapter<ImageInfo> adapter = new GridImageViewClickAdapter<ImageInfo>(this, arrayList);

        layout.setImageLoader(new GlideImageDisplay());
        layout.setAdapter(adapter);
    }

    public void exec(View view) {
        hideSoftKeyboard(this);

        String temp = ((EditText)findViewById(R.id.et_input)).getText().toString();

        try {
            num = Integer.parseInt(temp);
            if (num > 13) {
                Toast.makeText(this, "大哥，最多13张啊，给你显示默认的，哼", Toast.LENGTH_LONG).show();
                throw new Exception("");
            }
        } catch (Exception e) {
            num = 3;
        }

        initData();
    }

    public void send(View view) {
        startActivity(new Intent(this, SendMsgActivity.class));
    }

    public void send2(View view) {
        startActivity(new Intent(this, SendMsgActivityV2.class));
    }

    boolean r = false;
    public void send3(View view) {
        RotateAnimation animation;
        if (!r) {
            r = true;
            animation = new RotateAnimation(0, -30,
                    Animation.RELATIVE_TO_SELF, -1f, Animation.RELATIVE_TO_SELF, -1f);
            DecelerateInterpolator interpolator = new DecelerateInterpolator();
            animation.setInterpolator(interpolator);
            animation.setDuration(500);
            animation.setFillAfter(true);
            animation.setRepeatCount(0);
            imageView.startAnimation(animation);
        } else {
            r = false;
            imageView.clearAnimation();
            animation = new RotateAnimation(-30, 0,
                    Animation.RELATIVE_TO_SELF, -1f, Animation.RELATIVE_TO_SELF, -1f);
            DecelerateInterpolator interpolator = new DecelerateInterpolator();
            animation.setInterpolator(interpolator);
            animation.setDuration(500);
            animation.setFillAfter(true);
            animation.setRepeatCount(0);
            animation.setAnimationListener(new Animation.AnimationListener() {
                @Override
                public void onAnimationStart(Animation animation) {

                }

                @Override
                public void onAnimationEnd(Animation animation) {
                    Animation translateAnimation = new TranslateAnimation(0, 10, 0, 0);
                    translateAnimation.setInterpolator(new CycleInterpolator(3));
                    translateAnimation.setRepeatCount(0);
                    translateAnimation.setDuration(400);

                    view.startAnimation(translateAnimation);
                }

                @Override
                public void onAnimationRepeat(Animation animation) {

                }
            });
            imageView.startAnimation(animation);
        }


    }

    private void hideSoftKeyboard(Activity activity) {
        View view = activity.getCurrentFocus();
        if (view != null) {
            InputMethodManager inputMethodManager = (InputMethodManager) activity.getSystemService(Activity.INPUT_METHOD_SERVICE);
            inputMethodManager.hideSoftInputFromWindow(view.getWindowToken(), InputMethodManager.HIDE_NOT_ALWAYS);
        }
    }

    public void initStatusBar() {
        //Android全屏返回布局抖动问题
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
            getWindow().addFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS);
            int resourceId = getResources().getIdentifier("status_bar_height", "dimen", "android");
            // 获取状态栏高度
            int statusBarHeight = getResources().getDimensionPixelSize(resourceId);
            View rectView = new View(this);
            // 绘制一个和状态栏一样高的矩形，并添加到视图中
            LinearLayout.LayoutParams params
                    = new LinearLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, statusBarHeight);
            rectView.setLayoutParams(params);
            //设置状态栏颜色（该颜色根据你的App主题自行更改）
            rectView.setBackgroundColor(ContextCompat.getColor(this, R.color.purple_700));
            // 添加矩形View到布局中
            ViewGroup decorView = (ViewGroup) getWindow().getDecorView();
            decorView.addView(rectView);
            ViewGroup rootView = (ViewGroup) ((ViewGroup) this.findViewById(android.R.id.content)).getChildAt(0);
            rootView.setFitsSystemWindows(true);
            rootView.setClipToPadding(true);
        }
    }
}